package com.ihrm.company.service;

import com.ihrm.common.utils.IdWorker;
import com.ihrm.company.mapper.CompanyMapper;
import com.ihrm.domain.company.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CompanyService {

	@Autowired
	private CompanyMapper companyMapper;

	@Autowired
	private IdWorker idWorker;

	/**
	 * 保存企业
	 * 1.配置idwork到工程
	 * 2.在service中注入idwork
	 * 3.通过idwork生成id
	 * 4.保存企业
	 */
	public void add(Company company) {
		//基本属性的设置
		String id = idWorker.nextId() + "";
		company.setId(id);
		//默认的状态
		company.setAuditState("0");//0.未审核 1.审核通过
		company.setState(1);//0.未激活 1.已激活
		companyMapper.save(company);
	}


	/**
	 * 更新企业
	 * 1.参数：Company
	 * 2.根据id查询企业对象
	 * 3.设置修改的属性
	 * 4.调用mapper的更新方法
	 */
	public void update(Company company) {
		Company companyDb = companyMapper.findById(company.getId()).get();
		companyDb.setName(company.getName());
		companyDb.setCompanyPhone(company.getCompanyPhone());
		companyMapper.save(companyDb);
	}

	/**
	 * 删除企业
	 */
	public void deleteById(String id) {
		companyMapper.deleteById(id);
	}


	/**
	 * 根据id查询企业
	 */
	public Company findById(String id) {
		return companyMapper.findById(id).get();
	}

	/**
	 * 查询所有企业列表
	 */
	public List<Company> findAll() {
		return companyMapper.findAll();
	}
}
